package siii;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

import java.text.Format;
import java.util.Arrays;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.media.Buffer;
import javax.media.CaptureDeviceInfo;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.cdm.CaptureDeviceManager;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class CapturaFoto extends JPanel {


	public static Player player = null;
	public CaptureDeviceInfo di = null;  //  @jve:decl-index=0:
	public MediaLocator ml = null;  //  @jve:decl-index=0:
	public JButton capture = null;
	public Buffer buf = null;
	public Image img = null;
	public VideoFormat vf = null;
	public BufferToImage btoi = null;
	public ImagePanel imgpanel = null;

	private static final long serialVersionUID = 1L;
	private JButton captura = null;


	public CapturaFoto() {
		super();
		initialize();
	}


	private void initialize() {
		this.setLayout(new BorderLayout());
		this.setSize(327, 259);
		this.add(getCaptura(), BorderLayout.SOUTH);
		this.setVisible(true);


		String str2 = "vfw//0";
                Vector v = CaptureDeviceManager.getDeviceList(null);
                Vector v1 = CaptureDeviceManager.getDeviceList();
                //Vector v2 = CaptureDeviceManager.getDeviceList();

                String devicenames[] = new String [v.size()];
                Format formats[] = new Format[100];
                CaptureDeviceInfo cdi = null;

                for (int i = 0 ; i < v.size(); i++){
                    cdi = (CaptureDeviceInfo) v.elementAt(i);
                    System.out.println(cdi.getName());
                    System.out.println(Arrays.toString(cdi.getFormats()));
                }

		di = CaptureDeviceManager.getDevice(str2);
		ml =  new MediaLocator("vfw://0");
		try {
			player = Manager.createRealizedPlayer(ml);
			player.start();
			Component comp;
			if ((comp = player.getVisualComponent()) != null) {
				add(comp, BorderLayout.NORTH);
			}
			add(captura, BorderLayout.SOUTH);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void gravaImg (Image imagem){
//		String caminho = "C:\\Documents and Settings\\Patrick\\Desktop" +
//				"\\PontoEletronico\\Fotos\\"+"00"+(Integer.parseInt(Banco.
//						retornaGeral("select max(ID) from Funcionarios").toString())+1)+".JPG";
		
        	String caminho = "foto.JPG";
	
            try
                {

			ImageIO.write((RenderedImage) imagem, "jpg", new File(caminho));
			JOptionPane.showMessageDialog(this, "Imagem Capturada!");
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "não foi possivel encontrar " +
					"o dispositivo para a captura da imagem.");
			e.printStackTrace();
		}

	}
	private JButton getCaptura() {
		if (captura == null) {
			captura = new JButton("Captura");
			captura.setBounds(50, 50, 50, 50);
			captura.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					FrameGrabbingControl fgc = (FrameGrabbingControl) player
							.getControl("javax.media.control.FrameGrabbingControl");
					buf = fgc.grabFrame();
					btoi = new BufferToImage((VideoFormat) buf.getFormat());
					img = btoi.createImage(buf);
					gravaImg(img);
				}
			});
		}
		return captura;
	}

	class ImagePanel extends Panel {

		private static final long serialVersionUID = 1L;
		public Image myimg = null;

		public ImagePanel() {
			JOptionPane.showMessageDialog(null, "setando a classe");
			setLayout(null);
			setSize(320, 240);
		}

		public void setImage(Image img) {
			this.myimg = img;
			repaint();
		}

		public void paint(Graphics g) {
			if (myimg != null) {
				g.drawImage(myimg, 0, 0, this);
			}
		}

	}


                public static void main(String args[]){

                    CapturaFoto cf = new CapturaFoto();



                }

}